package sicnu.cs.ich.ich.aop;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.api.domain.entity.Ich;
import sicnu.cs.ich.ich.service.IIchRankService;
import sicnu.cs.ich.ich.service.IIchService;

import java.util.List;

/**
 * @author CaiKe
 * @date 2021/11/7 17:59
 */
@Aspect
@Slf4j
@Component
@AllArgsConstructor
public class IchHitAOP {
    private final IIchRankService ichService;

    /**
     * 拦截统计非遗类别热度
     *
     * @param joinPoint 切入点
     * @throws InterruptedException 异常
     */
    @Before("execution(public * sicnu.cs.ich.ich.service.IIchService.getIchsByTypeId(..))")
    public void doBeforeSelectType(JoinPoint joinPoint) throws Throwable {
        // 记录
        Object[] args = joinPoint.getArgs();
        ichService.ichTypeHitIncrementMQ((Integer) args[0]);
    }

    /**
     * 拦截统计非遗单品热度
     */
    @AfterReturning(value = "execution(public * sicnu.cs.ich.ich.service.IIchService.getIchById(..))", returning = "ich")
    public void doBeforeSelectIch(Ich ich) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        // id热度+1
//        log.info("非遗【{}】热度 +1", args[0]);
        ichService.ichHitIncrement(ich.getId());
        // 省级非遗热度提升
        ichService.ichProvinceIncrement(ich);
    }

}